home *** CD-ROM | disk | FTP | other *** search
- #ifndef _LIST_
- #define _LIST_
-
-
- template <class T>
- class LList
- {
- public:
- T *fListHead;
- T *fListTail;
- SInt32 fItemCount;
-
- inline LList(void) { fListHead = fListTail = NULL; fItemCount = 0; };
- inline ~LList(void) { };
-
- void Push(T *elem);
- inline T *Pop(void);
-
- inline void Append(T *elem);
- void Insert(T *after,T *elem);
- void Delete(T *elem);
-
- T *GetNthElement(UInt32 n);
-
- inline T *GetFirst(void) { return fListHead; };
- inline SInt32 isEmpty(void) { return (fItemCount == 0); };
- };
-
-
-
-
-
- template <class T> void LList<T>::Push(T *elem)
- {
- if (fListTail == NULL)
- fListTail = elem;
-
- elem->next = fListHead;
- fListHead = elem;
- fItemCount += 1;
- }
-
-
-
-
-
- template <class T> T *LList<T>::Pop(void)
- {
- T *elem;
-
-
- elem = fListHead;
- if (elem != NULL)
- {
- fListHead = elem->next;
-
- if (fListTail == elem)
- fListTail = fListHead;
-
- fItemCount -= 1;
- }
-
- return elem;
- }
-
-
-
-
-
- template <class T> void LList<T>::Append(T *elem)
- {
- if (fListTail != NULL)
- fListTail->next = elem;
- else
- fListHead = elem;
-
- elem->next = NULL;
- fListTail = elem;
- fItemCount += 1;
- }
-
-
-
-
-
- template <class T> void LList<T>::Insert(T *after,T *elem)
- {
- if (after == NULL)
- {
- Append(elem);
- return;
- }
-
- elem->next = after->next;
- after->next = elem;
-
- if (fListTail == after)
- fListTail = elem;
-
- fItemCount += 1;
- }
-
-
-
-
-
- template <class T> void LList<T>::Delete(T *elem)
- {
- T *walk,*next;
-
-
- if (elem == fListHead)
- {
- if (elem == fListTail)
- {
- fListHead = NULL;
- fListTail = NULL;
- }
- else
- fListHead = fListHead->next;
-
- fItemCount -= 1;
- }
- else
- {
- walk = fListHead;
- while((next = walk->next) != NULL)
- {
- if (next == elem)
- {
- walk->next = elem->next;
- if (elem == fListTail)
- fListTail = walk;
-
- fItemCount -= 1;
- return;
- }
-
- walk = next;
- }
- }
- }
-
-
-
-
-
- template <class T> T *LList<T>::GetNthElement(UInt32 n)
- {
- T *elem;
-
-
- elem = fListHead;
- while((elem != NULL) && (n > 0))
- {
- elem = elem->next;
- n -= 1;
- }
-
- return elem;
- }
-
-
- #endif /* _LIST_ */
-